﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace ODP.Helper
{
    public class FileUploadHelper
    {
        #region 上传文件
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="fileControl"></param>
        /// <param name="savePath"></param>
        /// <returns></returns>
        public string UploadFile(HttpFileCollection Files, string savePath,ref string fileName)
        {
            string result = string.Empty;
            var fileControl = Files[0];
            var fileNameArray = fileControl.FileName.Split("\\".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
            fileName = fileNameArray[fileNameArray.Length - 1];
            savePath = HttpRuntime.AppDomainAppPath + savePath ;

            result = savePath + fileName;

            try
            {
                if (!System.IO.Directory.Exists(savePath))
                {
                    System.IO.Directory.CreateDirectory(savePath);
                }

                if (!System.IO.File.Exists(result))
                {
                    fileControl.SaveAs(result);
                }
                else
                {
                    result = savePath + (new Random().Next(10000, 999999)).ToString() + "-" + fileName;
                    fileControl.SaveAs(result);
                }
            }
            catch
            {
                result = "";
            }
            
            return result;
        }
        #endregion
    }
}
